package org.example.likou150.circularList;

import java.util.*;

public class CopyRandomList {


    public Node copyRandomList(Node head) {
        Map<Node, Node> nodeMap = new HashMap<>();
        Node currentNode = head;
        while(currentNode!=null){
            nodeMap.put(currentNode, new Node(currentNode.val));
            currentNode=currentNode.next;
        }
        currentNode = head;
        while (currentNode!=null){
            Node newNode=nodeMap.get(currentNode);
            newNode.next=nodeMap.get(currentNode.next);
            newNode.random=nodeMap.get(currentNode.random);
            currentNode = currentNode.next;
        }

        return nodeMap.get(head);
    }


    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
}
